<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>标记分割字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.strstr.html">« strstr</a></li>
      <li style="float: right;"><a href="function.strtolower.html">strtolower »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>标记分割字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.strtok" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strtok</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strtok</span> &mdash; <span class="dc-title">标记分割字符串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.strtok-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strtok</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$token</code></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="simpara">可选 token （不支持命名参数）：</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>strtok</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$token</code></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>strtok()</strong></span> 将字符串 <code class="parameter">string</code> 分割为若干子字符串，每个子字符串以
   <code class="parameter">token</code> 中的字符分割。这也就意味着，如果有个字符串是 &quot;This is an example string&quot;，
   你可以使用空格字符作为 <code class="parameter">token</code>，将这句话分割成独立的单词。
  </p>
  <p class="para">
   注意，仅第一次调用 strtok 函数时才使用 <code class="parameter">string</code> 参数。
   随后每次调用 strtok 都将只使用 <code class="parameter">token</code> 参数，因为它会记住它在当前字符串中的位置。
   如果要重新开始分割一个新的字符串，你需要再次使用 <code class="parameter">string</code> 参数来调用 strtok 函数来对其进行初始化。
   注意，可以在 <code class="parameter">token</code> 参数中使用多个字符。字符串将被 <code class="parameter">token</code> 参数中任何一个字符分割。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    这个函数的行为可能与熟悉 <span class="function"><a href="function.explode.html" class="function">explode()</a></span> 的人期望略有不同。
    首先，在解析的字符串中，两个或多个连续的 <code class="parameter">token</code> 的
    字符被认为是单一的分隔符。
    此外，位于字符串开始或结束处的 <code class="parameter">token</code> 将被忽略。
    例如，如果使用一个字符串 <code class="literal">&quot;;aaa;;bbb;&quot;</code> ，连续调用 <span class="function"><strong>strtok()</strong></span>
    并将 <code class="literal">&quot;;&quot;</code> 作为一个 <code class="parameter">token</code>
    将返回字符串 &quot;aaa&quot; 和 &quot;bbb&quot;，然后返回 <strong><code>false</code></strong> 。
    因此，字符串将仅被拆分为两个元素，而 <code class="literal">explode(&quot;;&quot;, $string)</code> 将返回一个包含 5 个元素的数组。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strtok-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       被分成若干子字符串的原始<span class="type"><span class="type 字符串">字符串</span></span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">token</code></dt>

     <dd>

      <p class="para">
       分割 <code class="parameter">string</code> 时使用的分界字符。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.strtok-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   标记后的<span class="type"><span class="type 字符串">字符串</span></span>，如果没有更多标记可用，则返回 <strong><code>false</code></strong> 。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strtok-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4861">
    <p><strong>示例 #1 <span class="function"><strong>strtok()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is\tan&nbsp;example\nstring"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;使用制表符和换行符作为分界符&nbsp;*/<br /></span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&nbsp;\n\t"</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Word=</span><span style="color: #0000BB">$tok</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;\n\t"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4862">
    <p><strong>示例 #2 当 <span class="function"><strong>strtok()</strong></span> 找不到标记时的反应</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$first_token&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/something'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$second_token&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$first_token</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$second_token</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
    string(9) &quot;something&quot;
    bool(false)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4863">
    <p><strong>示例 #3 <span class="function"><strong>strtok()</strong></span> 和 <span class="function"><a href="function.explode.html" class="function">explode()</a></span> 的不同点</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">";aaa;;bbb;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$parts&nbsp;</span><span style="color: #007700">=&nbsp;[];<br /></span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">";"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$parts</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$tok</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">";"</span><span style="color: #007700">);<br />}<br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$parts</span><span style="color: #007700">),</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$parts&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">";"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$parts</span><span style="color: #007700">),</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
[&quot;aaa&quot;,&quot;bbb&quot;]
[&quot;&quot;,&quot;aaa&quot;,&quot;&quot;,&quot;bbb&quot;,&quot;&quot;]
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.strtok-notes">
  <h3 class="title">注释</h3>
  
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code>false</code></strong>，但也可能返回等同于 <strong><code>false</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.strtok-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>